package com.vanguard.service.impl;

import com.github.pagehelper.PageHelper;
import com.vanguard.commons.page.PageQuery;
import com.vanguard.commons.page.PageResult;
import com.vanguard.domain.User;
import com.vanguard.mapper.UserMapper;
import com.vanguard.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Title: 用户业务实现类
 * @Description:
 * @Author: vanguard
 * @Version: 1.0
 * @Date: 2019/03/21
 */
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public PageResult<User> getAll(PageQuery pageQuery) {
        //设置分页信息
        PageHelper.startPage(pageQuery.getPage(), pageQuery.getSize());
        List<User> users = userMapper.selectAll();
        PageResult<User> result = new PageResult<User>(users);
        return result;
    }

    @Override
    public User getById(Long id) {
        User user = userMapper.selectByPrimaryKey(id);
        return user;
    }

    @Override
    public void deleteById(Long id) {
        userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void save(User user) {
        if(user.getId() != null) {
            userMapper.updateByPrimaryKey(user);
        } else {
            userMapper.insert(user);
        }
    }


}
